#__auter:王梦丽
'''
爬数据
解析出来 代理ip
验证ip 能不能用
能用存起来 --》list,,文件，数据库
'''
from fake_useragent import UserAgent
import requests,re



class DaiLi:


    def get_all_url(self):

        all_url_list = []

        for i in range(1, 50):

            target_url = 'https://www.xicidaili.com/wn/{}'.format(i)

            all_url_list.append(target_url)

        print(all_url_list)

        return all_url_list



    def get_proxies(self):

        all_url_list = self.get_all_url()
        dl_list = []
        for url in all_url_list[5:6]:
            print(url)
            headers = {
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36'}

            proxies = {
                'http': '211.101.154.105:43598	',
            }
            info = requests.get(url, headers=headers, proxies=proxies)
            a = info.text
            # print(a)
            # Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36

            str1 = '(\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3})\D*(\d{1,5})'
            pattern = re.compile(str1)
            res = pattern.findall(a)
            # print(res)

            for i in res:
                # print(i[0])
                # print(i[1])
                headers = {
                    'User-Agent' :'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36'
                }
                proxies = {
                    # 'http':i[0]+':'+i[1],
                    'https': i[0]+':'+i[1]
                    }
                #豆瓣电影的url，判断是否能登录进去豆瓣电影
                douban_url = 'https://movie.douban.com/explore#!type=movie&tag=%E7%83%AD%E9%97%A8&sort=time&page_limit=20&page_start=0'
                try:
                    res = requests.get(douban_url, headers=headers, proxies=proxies, timeout=2)
                    if res.status_code == 200:
                        print('ip可用：', i[0], i[1])
                        dl_list.append(tuple((i[0], i[1])))
                        print(dl_list)
                except Exception as e:
                    print('ip有问题：', i[0], i[1])
        print(dl_list)

        return dl_list






if __name__ == '__main__':
    x = DaiLi()
    x.get_all_url()
    x.get_proxies()